﻿'use strict';

// todoPage controller

app.controller('efficiency', ['$stateParams','$state','$filter','$window','$http','$scope','$modal','toaster','$log', function($stateParams, $state, $filter, $window, $http, $scope, $modal, toaster, $log) {

    $scope.myChart = echarts.init(document.getElementById('efficiencyLine'));
    $scope.dataX = [];
    $scope.dataY = [];
    var dataShadow = [];
    var yMax = 5000;
    function getList(){
        toaster.getInfo('/a_yongan/list/goodsinformationchart',null).then(function(data){
            if(data && data.data && data.data.data){
                data.data.data.forEach(function(item){
                    $scope.dataX.push(item.name);
                    $scope.dataY.push(item.sl);
                });
                $scope.option.xAxis.data = $scope.dataX;
                $scope.option.series[0].data = $scope.dataY;
                for (var i = 0; i < $scope.dataY.length; i++) {
                    //dataShadow.push(yMax);
                }
                $scope.option.series[1].data = dataShadow;
                setOpition($scope.option);
            }

        });
    }
    function setOpition(option){
        console.log(option)
        if(!$scope.myChart){
            $scope.myChart = echarts.init(document.getElementById('efficiencyLine'));
        }
        $scope.myChart.setOption(
            option
        );
        //$scope.myChart.clear('click');
        var zoomSize = 6;
        $scope.myChart.on('click', function (params) {
            console.log(Math.min(params.dataIndex + zoomSize / 2, data.data.length - 1))
            $scope.myChart.dispatchAction({
                type: 'dataZoom',
                startValue: data.data[Math.max(params.dataIndex - zoomSize / 2, 0)].name,
                endValue: data.data[Math.min(params.dataIndex + zoomSize / 2, data.data.length - 1)].name
            });
        });
    }
    $scope.option = {
        title: {
            text: '图表分析',
            subtext: 'Inventory Icon Analysis'
        },
        tooltip: {
            axisPointer: {
                type: 'shadow'
            }
        },
        // xAxis: {
        //     data: [],
        //     axisLabel: {
        //         inside: true,
        //         textStyle: {
        //             color: '#fff'
        //         }
        //     },
        //     axisTick: {
        //         show: false
        //     },
        //     axisLine: {
        //         show: false
        //     },
        //     z: 10
        // },
        xAxis: {
            data:[]
        },
        yAxis: {
            axisLine: {
                show: false
            },
            axisTick: {
                show: false
            },
            axisLabel: {
                textStyle: {
                    color: '#999'
                }
            }
        },
        dataZoom: [
            {
                type: 'inside'
            }
        ],
        series: [
            {
                type: 'bar',
                itemStyle: {
                    normal: {
                        color: new echarts.graphic.LinearGradient(
                            0, 0, 0, 1,
                            [
                                {offset: 0, color: '#83bff6'},
                                {offset: 0.5, color: '#188df0'},
                                {offset: 1, color: '#188df0'}
                            ]
                        )
                    },
                    emphasis: {
                        color: new echarts.graphic.LinearGradient(
                            0, 0, 0, 1,
                            [
                                {offset: 0, color: '#2378f7'},
                                {offset: 0.7, color: '#2378f7'},
                                {offset: 1, color: '#83bff6'}
                            ]
                        )
                    }
                },
                data:[]
            },
            { // For shadow
                name:"库存数量",
                type: 'bar',
                itemStyle: {
                    normal: {color: 'rgba(0,0,0,0.05)'}
                },
                barGap:'-100%',
                barCategoryGap:'40%',
                data: [],
                animation: false
            }
        ]
    };

// Enable data zoom when user click bar.

    getList();
}])
;